global _start

section	.bss
buf	dd	1
oct	dd	1

section	.text
_start:
	call	getchar

	cmp	eax,-1
	je	.done
	
	call	hex_digit
	shl	eax,4
	mov	[oct],eax
	call	getchar
	call	hex_digit
	or	[oct],eax

	mov	eax,[oct]
	call	putchar
	
	jmp	_start

.done:
	mov	eax,0x01
	mov	ebx,0
	int	0x80

getchar:
	mov	eax,0x03
	mov	ebx,0		;stdin fd
	mov	ecx,buf
	mov	edx,1		;count
	int	0x80
	
	cmp	eax,1
	jne	.getchar_fail
	
	mov	eax,[buf]
	ret

.getchar_fail:
	mov	eax,-1
	ret

putchar:
	mov	[buf],eax

	mov	eax,0x04
	mov	ebx,1		;stdout fd
	mov	ecx,buf
	mov	edx,1
	int	0x80

	ret

hex_digit:
	cmp	eax,'0'
	jl	.next1
	
	cmp	eax,'9'
	jg	.next1
	
	sub	eax,'0'
	ret

.next1:
	cmp	eax,'a'
	jl	.next2
	
	cmp	eax,'f'
	jg	.next2
	
	sub	eax,('a'-10)
	ret

.next2:
	cmp	eax,'A'
	jl	.die
	
	cmp	eax,'F'
	jg	.die
	
	sub	eax,('A'-10)
	ret

.die
	mov	eax,0x01
	mov	ebx,2
	int	0x80

